/**
 * JavaScript Toolkit Copyright (C) 2008  Okan Akbulut
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

var Event = YAHOO.util.Event,
    Dom = YAHOO.util.Dom,
    lang = YAHOO.lang;


jst.TranslationService = function() {
    
    this.dictionary = new Object();
    
}

jst.TranslationService.prototype = {
    
    constructor: jst.TranslationService,
    
    translate: function(domain, target, msgid, text, mapping) {
        
        domain = domain || "default";
        
        target = target || this.negotiateLanguage() || "en";
        
        msgid = msgid || text;
        
        if(lang.isObject(this.dictionary[domain]) &&
           lang.isObject(this.dictionary[domain][target])) {
            
            msgid = msgid.replace(/\n/g, "").replace(/\b\s+/g," ");
            
            text = this.dictionary[domain][target][msgid] || text;            
        }
                
        if(lang.isObject(mapping)) {
            
            text = lang.substitute(text, mapping);
        }
        
        return text;
    },
    
    register: function(domain, language, catalog) {
        
        this.dictionary[domain] = this.dictionary[domain] || new Object();
        
        this.dictionary[domain][language] = this.dictionary[domain][language] || new Object();
        
        lang.augmentObject(this.dictionary[domain][language], catalog);
    },       
    
    negotiateLanguage: function() {
        
        return 'tr';
    }
}

var translation_service = new jst.TranslationService();

var i18n_name = "i18n:name",
    i18n_domain = "i18n:domain",
    i18n_target = "i18n:target",
    i18n_attributes = "i18n:attributes",
    i18n_translate = "i18n:translate";

jst.I18nPageTemplate = function(element, context, i18n) {
    
    this.i18n = i18n || new Object();
    
    this.i18n.mapping = this.i18n.mapping || new Object(); //customer:'Okan',
    
    jst.I18nPageTemplate.superclass.constructor.call(this, element, context);
}

YAHOO.lang.extend(jst.I18nPageTemplate, jst.PageTemplate, {
    
    constructor: jst.I18nPageTemplate,
        
    translate: function(msgid, text) {
        
        var domain = this.i18n.domain,
            lang = this.i18n.target,
            mapping = this.i18n.mapping ;
        
        return translation_service.translate(domain, lang, msgid, text, mapping);    
    },
    
    /**
     * Use translation variable
     */
    i18nName: function() {
        
        var name = this.element.getAttribute(i18n_name);
               
        if(name) {
            
            this.element.removeAttribute(i18n_name);
            
            var wrapper = document.createElement("div");
            
            wrapper.appendChild(this.element.cloneNode(true));
            
            this.i18n.mapping[name] = wrapper.innerHTML;
            
            var text = document.createTextNode("{"+name+"}");
            
            this.element.parentNode.replaceChild(text,this.element);
            
            return false;
        }
        
        return true;                
    },
    
    processChildNodes: function() {
        
        jst.I18nPageTemplate.superclass.processChildNodes.call(this);
        
        if(this.i18nName()) {
            
            this.i18nTranslate();
        }
    },
    
    /**
     * Set translation domain
     */
    i18nDomain: function() {
        
        var domain = this.element.getAttribute(i18n_domain);
           
        if(domain) {
            
            this.element.removeAttribute(i18n_domain);
          
            this.i18n.domain = lang.trim(domain);                  
        }  
    },
    
    /**
     * I18N Translate for specific language
     * @attribute i18n:target
     */
    i18nTarget: function() {
        
        var target = this.element.getAttribute(i18n_target);
        
        if(target) {
            
            this.element.removeAttribute(i18n_target);
            
            this.i18n.target = lang.trim(target);
        }
    },
    
    /**
     * HTML/XML attributes value translation
     * <img src="../icons/uparrow.png" alt="Up"
     *      i18n:attributes="src up-arrow-icon; alt up-arrow-alttext" />
     *   
     * @attribute  i18n:attributes
     */
    i18nAttributes: function() {
        
        var kvp = this.element.getAttribute(i18n_attributes);
            
        if(kvp) {
            
            this.element.removeAttribute(i18n_attributes);            
            
            var pair, attr, text, msgid, attributes = lang.trim(kvp).split(";");
            
            for(var i = 0; i < attributes.length; i++) {
                
                pair = attributes[i].split(" ");
                
                if(pair) {
                    
                    attr = lang.trim(pair[0]);
                 
                    text = lang.trim(this.element.getAttribute(attr));
                 
                    msgid = null;
                    
                    if(pair.length == 2) {
                        
                        msgid = lang.trim(pair[1]);
                    }
                    
                    text = this.translate(msgid, text);
                    
                    this.element.setAttribute(attr, text);                    
                }                
            }
        }        
    },
    
    /**
     * Translate HTML/XML element content
     */
    i18nTranslate: function() {
        
        var translate = this.element.getAttribute(i18n_translate);
                
        if(translate != null){
            
            this.element.removeAttribute(i18n_translate);
                
            var text = lang.trim(this.element.innerHTML),
                msgid = lang.trim(translate);
                            
            this.element.innerHTML = this.translate(msgid, text);
            
            //this.element.innerHTML = lang.substitute(text, this.i18n.mapping);                              
        }
                
    },
    
    setTextContent: function(text) {
                
        if(this.element.getAttribute(i18n_translate) != null){
            
            var msgid = this.element.getAttribute(i18n_translate);
            
            this.element.removeAttribute(i18n_translate);
            
            text = this.translate(msgid, text);
            
            jst.I18nPageTemplate.superclass.setTextContent.call(this, text);
        }
    },
    
    replaceWithTextNode: function(text) {
        
        var i18nName = this.element.getAttribute(i18n_name);       
        
        if(i18nName) {
            
            this.element.removeAttribute(i18n_name);
            
            i18nName = lang.trim(i18nName);
            
            this.i18n.mapping[i18nName] = text;
            
            jst.I18nPageTemplate.superclass.replaceWithTextNode.call(this, "{"+i18nName+"}")
        }
        else if(this.element.getAttribute(i18n_translate) != null){
            
            var msgid = this.element.getAttribute(i18n_translate);
            
            this.element.removeAttribute(i18n_translate);
            
            msgid = lang.trim(msgid);
            
            text = this.translate(msgid, text);
            
            jst.I18nPageTemplate.superclass.replaceWithTextNode.call(this, text);
        }
    },
    
    attributes: function() {
        
        jst.I18nPageTemplate.superclass.attributes.call(this);
        
        this.i18nAttributes();
    }
});

translation_service.register("default","tr", {
    "{name} was born in {country}.": "{name}, {country} de dogdu"
});

function init()
{
    //alert(navigator.userLanguage);    
    var pt = new jst.I18nPageTemplate(document.documentElement);

    pt.process();        
}

Event.onDOMReady(init);